// pages/list/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{getUserShopList} from '../../api/index'
import { baseURL } from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    pagesize:10,
    List:[],
    status:'数据加载中...',
    request_show:true,
    lat:'',
    lng:'',
    keyword:''
  },

  getList(){
    var that = this
    var obj = {
      uid:wx.getStorageSync('uid'),
      lat: that.data.lat,
      lng:that.data.lng,
      page:that.data.page,
      pagesize:that.data.pagesize, 
      keyword:this.data.keyword
      // status: e == 0 ? e : e + 1
    }
    getUserShopList(obj).then(res=>{
      if(res.msg.length <= 0){
        that.setData({
          status:'数据加载完毕！',
          request_show:false
        })
      }
  
      if(res.sta == 1){
          if(res.msg.length < that.data.pagesize){
            that.setData({
              status:'数据加载完毕！',
              request_show:false
            })
          }
  
          res.msg.forEach(item=>{
            var arr = []
            if(item.images){
              var arr1 = []
              arr = item.images.split(',')
              arr.forEach(item=>{
                arr1.push(baseURL + item)
              })
              item.images = arr1
            }
          })
        
          that.setData({
            List:[...that.data.List,...res.msg],
          })
      }
    })
  },
  onSearch(){
    this.setData({
      page:1,
      List:[],
      request_show:true
    })
    this.getList()
  },

  click(key){
    onClick(key,this)
  },
  input(key){
    onInput(key,this)
  },
  nav(key){
    navTo(key)
  },
  navtab(e){
    navToTab(e)
  },

  // 地址
  getsite(){
    const that = this
    wx.getLocation({
      type: "wgs84",
      success(res) {
        that.setData({
          lat:res.latitude,
          lng: res.longitude,
        })
        that.getList()
      }
  })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var carttotal = wx.getStorageSync('cart').length || 0
    wx.setTabBarBadge({
      index:2,
      text: String(carttotal)
    })
    this.getsite()
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var carttotal = wx.getStorageSync('cart').length || 0
    wx.setTabBarBadge({
      index:2,
      text: String(carttotal)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1,
      List:[],
      request_show:true
    })
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page:this.data.page + 1,
    })
    if(this.data.request_show){
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})